package com.cykj.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.extra.mail.MailAccount;
import cn.hutool.extra.mail.MailUtil;
import com.cykj.util.EmailUtil;
import com.cykj.util.ImageCodeUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/code")
public class CodeController {
    @RequestMapping("/createCode")
    public void createCode(HttpServletResponse response, HttpSession session) {
        //创建
        MailAccount account = new MailAccount();
        account.setHost(EmailUtil.emailSMTPHost);
        account.setPort(EmailUtil.emailPort);
        account.setAuth(true);
        account.setStarttlsEnable(true); // 必须

        account.setFrom(EmailUtil.emailAccount); // 发件人（必须正确，否则发送失败，报：Invalid Addresses: null）
        account.setUser(EmailUtil.emailUser);         // 用户名，默认为发件人邮箱前缀，可以不填，也可以填发件人邮箱全称
        account.setPass(EmailUtil.emailPassword);         // 腾讯邮箱授权码
        ImageCodeUtils imageCodeUtils = new ImageCodeUtils();

        //获取验证码
        String code = imageCodeUtils.getCode();
        //将验证码存session中
        session.setAttribute("code",code);
        //设置有效时间
        session.setMaxInactiveInterval(60);

        //发送邮箱
        MailUtil.send(account, CollUtil.newArrayList("429490429@qq.com"),"健景体检系统登录验证码",
                "欢迎登录体检系统，您的登录验证码是<p style='color:red;'>"+ code +"</p>有效时间为一分钟", true);
        System.out.println("code------"+code);
        System.out.println("session----------"+session.getAttribute("code"));

    }



}
